/**
 * 
 */
package com.maywide.jdp.system.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.maywide.jdp.core.vo.KeyValue;
import com.maywide.jdp.core.vo.Page;
import com.maywide.jdp.system.annotation.JSON;
import com.maywide.jdp.system.bean.DirectoryBean;
import com.maywide.jdp.system.entities.Directory;
import com.maywide.jdp.system.entities.Domain;
import com.maywide.jdp.system.utils.JSONEditor;

/**
 * 字典管理Service
 * 
 * @author luoyang
 * @version 1.0
 * @since JDK1.6
 */
@Controller("directoryService")
public class DirectoryServiceImpl implements DirectoryService {
	/**
	 * 字典管理Bean接口
	 */
	private DirectoryBean bean = null;

	/**
	 * @param bean
	 *            the bean to set
	 */
	@Autowired
	@Qualifier("directoryBean")
	public void setBean(DirectoryBean bean) {
		this.bean = bean;
	}

	@JSON
	@RequestMapping(value = "/system/directory/insert.json")
	@Override
	public Directory insert(
			@RequestParam(value = "directory") Directory directory,
			HttpServletRequest request, HttpServletResponse response,
			HttpSession session) throws Exception {
		bean.insert(directory);
		return directory;
	}

	@JSON
	@RequestMapping(value = "/system/directory/update.json")
	@Override
	public Directory update(
			@RequestParam(value = "directory") Directory directoryVO,
			HttpServletRequest request, HttpServletResponse response,
			HttpSession session) throws Exception {
		String id = directoryVO.getId();
		Directory directory = (Directory) bean.load(id);
		// 取得不可改变属性
		Domain domain = directory.getDomain();
		directoryVO.setDomain(domain);

		BeanUtils.copyProperties(directoryVO, directory);

		bean.update(directory);
		return directory;
	}

	@JSON
	@RequestMapping(value = "/system/directory/load.json")
	@Override
	public Directory load(@RequestParam(value = "id") String id,
			HttpServletRequest request, HttpServletResponse response,
			HttpSession session) throws Exception {
		Directory directory = (Directory) bean.load(id);
		return directory;
	}

	@RequestMapping(value = "/system/directory/lock.json")
	@Override
	public void lock(@RequestParam(value = "id") String id,
			HttpServletRequest request, HttpServletResponse response,
			HttpSession session) throws Exception {
		bean.lock(id);
	}

	@RequestMapping(value = "/system/directory/unlock.json")
	@Override
	public void unlock(@RequestParam(value = "id") String id,
			HttpServletRequest request, HttpServletResponse response,
			HttpSession session) throws Exception {
		bean.unlock(id);
	}

	@RequestMapping(value = "/system/directory/remove.json")
	@Override
	public void remove(@RequestParam(value = "id") String id,
			HttpServletRequest request, HttpServletResponse response,
			HttpSession session) throws Exception {
		bean.remove(Directory.class, id);
	}

	@JSON
	@RequestMapping(value = "/system/directory/combo.json")
	public List<KeyValue> combo(@RequestParam(value = "code") String code,
			HttpServletRequest request, HttpServletResponse response,
			HttpSession session) throws Exception {
		return bean.findByDomainCode(code);
	}

	@JSON
	@RequestMapping(value = "/system/directory/page.json")
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Page page(
			@RequestParam(value = "first", defaultValue = "0") int first,
			@RequestParam(value = "limit", defaultValue = "20") int limit,
			@RequestParam(value = "sort", required = false) String sort,
			@RequestParam(value = "dir", required = false) String dir,
			@RequestParam(value = "domainID", required = false) String domainID,
			@RequestParam(value = "id", required = false) String id,
			@RequestParam(value = "key", required = false) String key,
			@RequestParam(value = "active", required = false) String active,
			HttpServletRequest request, HttpServletResponse response,
			HttpSession session) throws Exception {
		Map conditions = new HashMap();

		if (key != null) {
			key = new StringBuffer("%").append(key).append("%").toString();
			conditions.put("key", key);
		}

		if (active != null)
			conditions.put("active", Boolean.parseBoolean(active));

		if (domainID != null && !"domain_root".equals(domainID)
				&& !"dir_domain_root".equals(domainID))
			conditions.put("domainID", domainID);
		else
			conditions.put("noDomainID", null);

		return bean.page(first, limit, sort, dir, conditions, null);
	}

	@InitBinder
	protected void initBinder(WebDataBinder binder) {
		binder.registerCustomEditor(Directory.class, new JSONEditor(
				Directory.class));
	}
}
